import 'package:flutter/material.dart';
import 'package:flutter_picker_plus/picker.dart';
import 'package:spendbook/models/family.dart';

IndexedWidgetBuilder listSeparatorBuilder = (BuildContext context, int index) => Divider(
      height: 1.0,
    );

class PublicList extends StatefulWidget {
  PublicList({Key? key, required this.children}) : super(key: key);

  final List<Widget> children;

  @override
  _PublicListState createState() => _PublicListState();
}

class _PublicListState extends State<PublicList> {
  @override
  Widget build(BuildContext context) {
    return ListView.separated(
      itemCount: widget.children.length,
      separatorBuilder: listSeparatorBuilder,
      itemBuilder: (BuildContext context, int index) {
        return widget.children[index];
      },
    );
  }
}

Future selectFamily({
  required BuildContext context,
  Function? onSubmit,
  Function? onCancel,
}) async {
  List _list = await Family().fetch();
  List<Family> _families = [];
  _list.forEach((_item) => _families.add(Family()..fromJson(_item)));
  List<String> _data = [];
  _families.forEach((f) => _data.add(f.nickname));

  return await Picker(
      adapter: PickerDataAdapter<String>(
        pickerData: _data,
      ),
      confirmText: '确定',
      cancelText: '看自己',
      onCancel: () async {
        if (onCancel != null) {
          await onCancel();
        }
      },
      onConfirm: (Picker picker, List value) async {
        if (onSubmit != null) {
          await onSubmit(_families.elementAt(value[0]));
        }
      }).showModal(context);
}

class NoData extends StatelessWidget {
  NoData({Key? key, this.content = '没有数据哦!'}) : super(key: key);

  final String content;

  @override
  Widget build(BuildContext context) {
    return Container(
      height: MediaQuery.of(context).size.height / 2,
      alignment: Alignment.center,
      child: Text(
        content,
        style: TextStyle(fontFamily: 'HappyFont', fontSize: 16),
      ),
    );
  }
}

class ListTileScope extends StatelessWidget {
  ListTileScope({
    Key? key,
    this.leading,
    this.title,
    this.subtitle,
    this.trailing,
    this.isThreeLine = false,
    this.readonly = false,
    this.dense,
    this.onTap,
  }) : super(key: key);

  final Widget? leading;

  final Widget? title;

  final Widget? subtitle;

  final Widget? trailing;

  final bool? dense;

  final bool readonly;

  final bool isThreeLine;

  final GestureTapCallback? onTap;

  @override
  Widget build(BuildContext context) {
    return readonly
        ? ListTile(
            key: key,
            leading: leading,
            title: title,
            subtitle: subtitle,
            dense: dense,
            isThreeLine: isThreeLine,
          )
        : ListTile(
            key: key,
            leading: leading,
            title: title,
            subtitle: subtitle,
            dense: dense,
            isThreeLine: isThreeLine,
            trailing: trailing,
            onTap: onTap,
          );
  }
}
